<template>
  <el-container>
    <el-header>
      <div style="margin-top: 15px;">
        <el-input v-model="keyword" placeholder="物质编码/CAS号码" class="input-with-select">
          <el-select slot="prepend" v-model="type" placeholder="请选择">
            <el-option label="染料" value="染料" />
            <el-option label="助剂" value="助剂" />
          </el-select>
          <el-button slot="append" icon="el-icon-search" @click="findPage(null)" />
        </el-input>
      </div>
    </el-header>
    <el-dialog title="编辑属性" :visible.sync="elementModifyFlag" width="80%">
      <element-modify :element-data="elementData" />
    </el-dialog>
    <el-main>
      <!-- <el-table v-show="resultFlag" :data="pageResult" border style="width: 100%">
        <el-table-column fixed prop="baseinfoWzbm" label="物质编码" />
        <el-table-column prop="baseinfoZwmc" label="中文名称" />
        <el-table-column prop="baseinfoYwmc" label="英文名称" />
        <el-table-column prop="baseinfoCas" label="CAS号" />
        <el-table-column prop="baseinfoFzs" label="分子式" />
        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="queryDetail(scope.row.baseinfoWzbm)">查看</el-button>
            <el-button v-if="editFlag" type="text" size="small" @click="modify(scope.row.baseinfoWzbm)">编辑</el-button>
            <el-button v-if="editFlag" type="text" size="small" @click="deleteElement(scope.row.baseinfoWzbm)">删除</el-button>
          </template>
        </el-table-column>
      </el-table> -->
      <kt-table
        :height="650"
        perms-view="admin"
        :perms-edit="currentUser"
        :perms-delete="currentUser"
        :data="pageResult"
        :columns="columns"
        @findPage="findPage"
        @handleView="queryDetail"
        @handleEdit="modify"
        @handleDelete="deleteElement"
      />
    </el-main>
    <el-dialog class="abow_dialog" title="元素详情" :visible.sync="detailFlag" width="80%">
      <element-detail :element-id="queryId" />
    </el-dialog>
  </el-container>

</template>

<script>
import KtTable from '@/views/Core/KtTable'
import ElementDetail from '@/views/Element/ElementDetail'
import ElementModify from './ElementModify'
export default {
  name: 'Element',
  components: {
    ElementModify,
    ElementDetail,
    KtTable
  },
  data() {
    return {
      elementModifyFlag: false,
      keyword: '',
      type: '染料',
      pageRequest: {
        pageNumber: 1,
        pageSize: 15
      },
      pageResult: {},
      resultFlag: false,
      detailFlag: false,
      queryId: '',
      elementData: {},
      currentUser: 'guest',
      columns: [
        { prop: 'baseinfoWzbm', label: '物质编码', minWidth: 40 },
        { prop: 'baseinfoZwmc', label: '中文名称', minWidth: 50 },
        { prop: 'baseinfoYwmc', label: '英文名称', minWidth: 50 },
        { prop: 'baseinfoCas', label: 'CAS号', minWidth: 100 },
        { prop: 'baseinfoFzs', label: '分子式', minWidth: 150 }

      ]
    }
  },
  mounted() {
    var user = sessionStorage.getItem('user')
    this.currentUser = user
  },
  methods: {
    findPage: function(data) {
      if (data !== null) {
        this.pageRequest = data.pageRequest
      }
      this.pageRequest.keyword = this.keyword
      this.pageRequest.type = this.type
      this.$api.element.query(this.pageRequest).then((res) => {
        this.pageResult = res.data
        this.resultFlag = true
      }).then(data != null ? data.callback : '')
    },
    queryDetail: function(params) {
      const id = params.row.baseinfoWzbm
      console.log(id)
      this.detailFlag = true
      this.queryId = id
    },
    modify: function(params) {
      const id = params.row.baseinfoWzbm
      this.$api.element.findById(id).then((res) => {
        this.elementModifyFlag = true
        this.elementData = res.data
      })
    },
    deleteElement: function(params) {
      console.log(params)
      const id = params.row.baseinfoWzbm
      this.$confirm('此操作将永久删除该元素, 是否继续?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.element.delById(id).then((res) => {
          this.findPage(null)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}

</script>

<style>
  .el-select .el-input {
    width: 130px;
  }

  .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }
</style>
